<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

</head>


<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->

                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteContent()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">

                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th>
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>
									      <th class="sorting">图片</th>
									      <th class="sorting">排序</th>
									      <th class="sorting">是否有效</th>
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">

			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->


<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告增加</h3>
		</div>
		<div class="modal-body">
			<form id="add_form">
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" name="categoryId" id="select">
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" id="title" name="title" placeholder="标题" >  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" id="url" name="url" placeholder="URL" >  </td>
		      	</tr>
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" id="sortOrder" name="sortOrder" placeholder="排序" >  </td>
		      	</tr>
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<div class="form-group">
									<label for="myFile">上传文件</label>
									<input type="file" id="myFile" name="pic" multiple/>
									<input type="text" name="pic" id="pic">
								</div>
							</tr>
						</table>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="checkbox" value="1" name="status" id="status" class="icheckbox_square-blue" >
		      		</td>
		      	</tr>
			 </table>
			</form>
		</div>
		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 编辑窗口 -->
  <div class="modal fade" id="editModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">广告编辑</h3>
			  </div>
			  <div class="modal-body">
				  <form id="update_form">
					  <table class="table table-bordered table-striped"  width="800px">
						  <tr>
							  <td>广告分类</td>
							  <td>
								  <select class="form-control" name="categoryId" id="select1">
								  </select>
							  </td>
						  </tr>
						  <tr>
							  <td>标题</td>
							  <td><input  class="form-control" type="hidden" id="id" name="id" placeholder="标题" >
								  <input  class="form-control" id="title1" name="title" placeholder="标题" >  </td>
						  </tr>
						  <tr>
							  <td>URL</td>
							  <td><input  class="form-control" id="url1" name="url" placeholder="URL" >  </td>
						  </tr>
						  <tr>
							  <td>排序</td>
							  <td><input  class="form-control" id="sortOrder1" name="sortOrder" placeholder="排序" >  </td>
						  </tr>
						  <tr>
							  <td>广告图片</td>
							  <td>
								  <table>
									  <tr>
										  <div class="form-group">
											  <label for="myFile">上传文件</label>
											  <input type="file" id="myFile1" name="pic" multiple/>
											  <input type="text" name="pic" id="pic1">
										  </div>
									  </tr>
								  </table>
							  </td>
						  </tr>
						  <tr>
							  <td>是否有效</td>
							  <td>
								  <input type="checkbox" value="1" name="status" id="status1" class="icheckbox_square-blue" >
							  </td>
						  </tr>
					  </table>
				  </form>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="update()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
</body>
<link rel="stylesheet" type="text/css" href="../js/bootstrap-fileinput/css/fileinput.css">
<script type="text/javascript" src="../js/bootstrap-fileinput/js/fileinput.js"></script>
<script type="text/javascript" src="../js/bootstrap-fileinput/js/locales/zh.js"></script>
</html>
<script>
	$(function (){

		contentList();

		findCategory();
		//图片
		$('#myFile').fileinput({
			language: 'zh',
			uploadUrl: '/upload',
			maxFileCount : 5,
			enctype : "multipart/form-data",
			maxFileSize : 1024,
			allowedFileExtensions : ["jpg", "png", "gif"],
			enctype : "multipart/form-data",
			browseClass: 'btn btn-primary',
		});


		$("#myFile").on("fileuploaded", function(event, data, previewId, index){

			$("#pic").val(data.response.data);
		})



	})
	//查询
	function contentList(){
		$.ajax({
			url:"/content/contentList",
			type:"post",
			dataType:"json",
			data:{},
			async:false,
			success:function (result){
				if(result.code == 10000) {
					var data = result.data;
					var tr='';
					for(var i=0;i<data.length;i++){
						tr+='<tr>';
						tr+='<td><input id="box" value="'+data[i].id+'" type="checkbox"></td>';
						tr+='<td>'+data[i].id+'</td>';
						tr+='<td>'+Category(data[i].categoryId)+'</td>';
						tr+='<td>'+data[i].title+'</td>';
						tr+='<td>'+data[i].url+'</td>';
						tr+='<td>';
						tr+='<img src="'+data[i].pic+'" width="100px" height="50px">';
						tr+='</td>';
						tr+='<td>'+data[i].sortOrder+'</td>';
						tr+='<td>';
						var st =null
						if(data[i].status=="1"){
							st="有效"
						}else if(data[i].status== null){
							st="无效"
						}
						tr+= st;
						tr+='</td>';
						tr+='<td class="text-center">';
						tr+='<button type="button" class="btn bg-olive btn-xs" onclick="getById('+data[i].id+')" data-toggle="modal" data-target="#editModal1">修改</button>';
						tr+='</td>';
						tr+='</tr>';

					}
					$("#tbody").html(tr);
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
	//分类查询展示
	function Category(id){
		var name;
		$.ajax({
			url:"/contentCateGory/getContentCatGoryById",
			type:"post",
			dataType:"json",
			data:{id:id},
			async:false,
			success:function (result){
				if(result.code == 10000) {

					name = result.data.name

				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
		return name;
	}

	//类型下拉框
	function findCategory(){
		$.ajax({
			url:"/content/findCategory",
			type:"post",
			dataType:"json",
			data:{},
			async:false,
			success:function (result){
				if(result.code == 10000) {
					var data = result.data;
					var option='';
					for(var i=0;i<data.length;i++){
						option+='<option value="'+data[i].id+'" id="categoryId">'+data[i].name+'</option>';
					}
					$("#select").html(option);
					$("#select1").html(option);
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
	//增加
	function add(){
		$.ajax({
			url:"/content/save",
			type:"post",
			dataType:"json",
			data:$("#add_form").serialize(),
			async:false,
			success:function (result){
				if(result.code == 10000) {
					alert("增加成功")
					location.reload()
				}else{
					alert("增加失败")
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
	//回显
	function getById(id){
		$.ajax({
			url:"/content/getById",
			type:"post",
			dataType:"json",
			data:{id:id},
			async:false,
			success:function (result){
				if(result.code == 10000) {
					var data = result.data;
					$("#id").val(data.id)
					$("#select1").val(data.categoryId)
					$("#title1").val(data.title)
					$("#url1").val(data.url)
					$("#pic1").val(data.pic)
					$("#sortOrder1").val(data.sortOrder)
					if(data.status == 1){
						$("#status1").prop("checked",true);
					}
					//图片
					$('#myFile1').fileinput({
						language: 'zh',
						uploadUrl: '/upload',
						maxFileCount : 5,
						enctype : "multipart/form-data",
						maxFileSize : 1024,
						allowedFileExtensions : ["jpg", "png", "gif"],
						enctype : "multipart/form-data",
						browseClass: 'btn btn-primary',
						initialPreviewAsData: true,//是否初始预览图片
						initialPreviewFileType: 'image',//回显文件类型(初始预览文件类型)
						initialPreview: data.pic,//图片回显(初始预览图片地址)
					});


					$("#myFile1").on("fileuploaded", function(event, data, previewId, index){

						$("#pic1").val(data.response.data);
					})
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
	//修改
	function update(){
		$.ajax({
			url:"/content/contentUpdate",
			type:"post",
			dataType:"json",
			data:$("#update_form").serialize(),
			async:false,
			success:function (result){
				if(result.code == 10000) {
					alert("修改成功")
					location.reload()
				}else{
					alert("修改失败")
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}

	//删除
	function deleteContent(){
		var ids = $("#box:checked");
		var idsList = [];
		for (var i=0;i<ids.length;i++){
			idsList.push(ids[i].value);
		}
		$.ajax({
			url:"/content/deleteContent",
			type:"post",
			dataType:"json",
			data:{id:idsList},
			async:false,
			success:function (result){
				if(result.code == 10000) {
					alert("删除成功")
					location.reload();
				}else{
					alert("删除失败")
				}
			},
			error:function (data){
				alert("网络异常")
			}
		})
	}
</script>
